package com.atguigu.activity;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务分配有三种方式(分配给具体用户)
 *
 * 1. 固定分配
 * 2. UEL表达式分配
 * 3. 监听器分配
 */
@SpringBootTest
public class ActivityTest2 {
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private HistoryService historyService;
    /**
     *
     * @author lyj
     * @date 2024-01-14
     * 流程定义部署1
     * UEL表达式分配 UEL-VALUE
     */
    @Test
    public void deployProcess(){
        Deployment deploy = repositoryService.createDeployment().addClasspathResource("process/qingjia1.bpmn20.xml").name("请假申请流程2").deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }
    /**
     *
     * @author lyj
     * @date 2024-01-14
     * 流程定义部署2
     * UEL表达式分配 UEL-METHOD
     */
    @Test
    public void deployProcess1(){
        Deployment deploy = repositoryService.createDeployment().addClasspathResource("process/qingjia2.bpmn20.xml").name("请假申请流程2").deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }
    /**
     *
     * @author lyj
     * @date 2024-01-14
     * 流程定义部署3
     * 监听器分配
     */
    @Test
    public void deployProcess2(){
        Deployment deploy = repositoryService.createDeployment().addClasspathResource("process/qingjia3.bpmn20.xml").name("请假申请流程3").deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }
    /**
     * 启动流程实例1
     * UEL表达式分配 UEL-VALUE
     */
    @Test
    public void startUpProcess01() {
        Map<String,Object> map=new HashMap<>();
        map.put("assignee1","zhangsan");
        map.put("assignee2","lisi");
        runtimeService.startProcessInstanceByKey("qingjia1",map);
    }
    /**
     * 启动流程实例2
     * UEL表达式分配 UEL-METHOD
     */
    @Test
    public void startUpProcess02() {

        runtimeService.startProcessInstanceByKey("qingjia2");
    }
    /**
     * 启动流程实例3
     * 监听器分配
     */
    @Test
    public void startUpProcess03() {

        runtimeService.startProcessInstanceByKey("qingjia3");
    }
    /**
     * 完成任务
     */
    @Test
    public void completeTask(){
        String assignee="lisi";
        //返回一条信息,如果当前用户需要处理多条任务会报错
        List<Task> list = taskService.createTaskQuery().taskAssignee(assignee).list();
        for (Task task : list) {
            taskService.complete(task.getId());
        }
    }


}
